#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // 读取图像
    std::string image_path = "../LingXiao.png";
    cv::Mat img = cv::imread(image_path);

    // 检查图像是否成功读取
    if (img.empty()) {
        std::cerr << "Error: Image not found or unable to open the image at path: " << image_path << std::endl;
        return 1;
    }

    // 图像的基本信息
    std::cout << "Type: " << typeid(img).name() << std::endl;  // 数据类型
    std::cout << "Shape (h, w, c): (" << img.rows << ", " << img.cols << ", " << img.channels() << ")" << std::endl;  // 高、宽、通道数
    std::cout << "Size (pixels): " << img.total() << std::endl;  // 像素总数
    std::cout << "Dtype: " << img.type() << std::endl;  // 数据类型标识符

    // 访问图像的像素信息
    int x = 100, y = 100;
    cv::Vec3b pixel_value = img.at<cv::Vec3b>(y, x);
    std::cout << "Pixel value at (" << x << ", " << y << "): [B, G, R] = [" 
              << (int)pixel_value[0] << ", " << (int)pixel_value[1] << ", " << (int)pixel_value[2] << "]" << std::endl;
    std::cout << "B channel value: " << (int)pixel_value[0] << std::endl;
    std::cout << "G channel value: " << (int)pixel_value[1] << std::endl;
    std::cout << "R channel value: " << (int)pixel_value[2] << std::endl;

    // 修改图像的某个像素值
    img.at<cv::Vec3b>(0, 100) = cv::Vec3b(0, 0, 255);  // 设置为红色

    // 图像裁剪 (y0:y1, x0:x1)
    int y0 = 116, y1 = 150, x0 = 343, x1 = 385;
    cv::Mat crop_img = img(cv::Rect(x0, y0, x1 - x0, y1 - y0)).clone();

    // 拆分和合并图像通道
    std::vector<cv::Mat> channels;
    cv::split(img, channels);
    cv::merge(channels, img);

    // 制作图像边界（填充）
    int top = 50, bottom = 50, left = 10, right = 10;
    cv::Mat replicate;
    cv::copyMakeBorder(img, replicate, top, bottom, left, right, cv::BORDER_REPLICATE);

    // 显示图像
    cv::imshow("LingXiao", img);
    cv::imshow("Crop Image", crop_img);
    cv::imshow("Replicated Border", replicate);

    // 等待按键
    std::cout << "Press ESC to close the windows." << std::endl;
    while (true) {
        int key = cv::waitKey(0);
        if (key == 27) {  // ESC 键
            break;
        } else {
            std::cout << "Key pressed: " << key << std::endl;
        }
    }

    // 关闭所有窗口
    cv::destroyAllWindows();
    return 0;
}
